package com.tianji.learning.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.tianji.common.exceptions.BizIllegalException;
import com.tianji.learning.domain.po.PointsBoardSeason;
import com.tianji.learning.mapper.PointsBoardSeasonMapper;
import com.tianji.learning.service.IPointsBoardSeasonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 东哥
 * @since 2025-07-06
 */
@Slf4j
@Service
public class PointsBoardSeasonServiceImpl extends ServiceImpl<PointsBoardSeasonMapper, PointsBoardSeason> implements IPointsBoardSeasonService {

    /**
     * 1、根据日期查询赛季ID
     *
     * @param lastMonthDate
     * @return
     */
    @Override
    public Integer querySeansonIdByDate(LocalDate lastMonthDate) {
        //-- begin end
        // select * from`learning_lesson` where create_time >= '2025-07-01'  and create_time <= '2025-07-31'

        // -- date
        // select * from `points_board_season` where begin_time <= '2025-07-31' and end_time >= '2025-07-31'

        PointsBoardSeason season = lambdaQuery()
                .le(PointsBoardSeason::getBeginTime, lastMonthDate)
                .ge(PointsBoardSeason::getEndTime, lastMonthDate)
                .one();
        if (ObjectUtil.isNull(season)) {
            log.error("指定日期对应的，赛季信息不存在：{}", lastMonthDate.toString());
            throw new BizIllegalException("指定日期对应的赛季信息 : {" + lastMonthDate + "}不存在");
        }
        return season.getId();
    }
}
